package com.hearwego;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.utilities.HWGSQLiteHelper;
import com.utilities.Informacion;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class TabFragmentHeader extends Fragment {
	
	ImageView imagen_header;
	TextView nombre_header, direccion_header;
	ProgressDialog dialog, dialog1;
	Drawable d;
	View view;
	HWGSQLiteHelper usdbh;
	SQLiteDatabase db;
	String poi_nombre, poi_direccion;
	ImageView imagen_foto;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_tab_fragment_header, container, false);
		new LoagGastos().execute("");
		return view;
	}
	
	public class LoagGastos extends AsyncTask<String, String, String>{
    	int zeroGastos = -1;
    	
    	@Override
    		protected void onPreExecute() {
    			super.onPreExecute();
    		}
    	
		@Override
		protected String doInBackground(String... params) {
			usdbh =new HWGSQLiteHelper(getActivity(), "DBHWG", null, 1);
	        db = usdbh.getWritableDatabase();
	        
	        Informacion informacion = new Informacion();
	        
	        if(db != null)
	        {
	        	Cursor c = db.rawQuery("SELECT * FROM Detalle", null);
	        	if (c.moveToFirst()) {
	        		do{
	        		 informacion.setNombre(c.getString(2));	
	        		 //poi_nombre = c.getString(2);
	        		 poi_direccion = c.getString(4);
						if(c.getBlob(c.getColumnIndexOrThrow("bitmapfoto"))!= null){
							byte[] bb = c.getBlob(c.getColumnIndexOrThrow("bitmapfoto"));
							informacion.setBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
							Log.i("BLOB ", ""+bb + c.getColumnIndexOrThrow("bitmapfoto"));
							Log.i("BLOB INFO DENTRO ", ""+informacion.getBitmap());
						}
	        		}while (c.moveToNext());
	        		Log.i("BD",poi_nombre + poi_direccion );
	        	}else{
	        		zeroGastos = 0;
	        	}
	        	
	        	db.close();
	        }
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Informacion info = new Informacion();
			imagen_foto = (ImageView) view.findViewById(R.id.imagen_poi_description);
			imagen_foto.setVisibility(View.VISIBLE);
			Log.i("BLOB INFO", ""+info.getBitmap() );

			
			//imagen_foto.setImageBitmap(info.getBitmap());

			nombre_header = (TextView) view.findViewById(R.id.nombre_header);
			nombre_header.setVisibility(View.VISIBLE);
			
			nombre_header = (TextView) view.findViewById(R.id.nombre_header);
			nombre_header.setVisibility(View.VISIBLE);
			direccion_header = (TextView) view.findViewById(R.id.direccion_header);
			direccion_header.setVisibility(View.VISIBLE);
			
			//nombre_header.setText(poi_nombre);
			nombre_header.setText(info.getNombre());
			direccion_header.setText(poi_direccion);

		}
    	
    }
}
